package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.data.MyChuTiAdapter;
import com.xgkj.diyiketang.data.DatiInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.DeleteTiMuTask;
import com.xgkj.diyiketang.task.MyQuestionListTask;
import com.xgkj.diyiketang.util.NoDataUtil;
import com.xgkj.diyiketang.weidget.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XCKJ_Paladowow on 2016/10/9.
 */
public class MyChuTiActivity extends BaseActivity  implements PullToRefreshListView.OnRefreshListener,PullToRefreshListView.OnLoadDataListener,MyChuTiAdapter.OnAdapterClickListener {
    private Context mContext;
    private PullToRefreshListView mListView;
    private MyChuTiAdapter mAdapter;
    private int currentPage = 1;
    private int size = 20;
    private int myzhiboListTaskId = -1;
    private int deleteZhiboTaskId = -1;
    private NoDataUtil noDataUtil;
    private String mUserId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_chuti_list);
        mContext = this;

        View view = findViewById(R.id.content_layout);
        mUserId = getEatShowApp().getmUserInfo().getUserId();
        initView();
        noDataUtil = new NoDataUtil(mContext,view);
    }

    private void initView(){
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        View view = findViewById(R.id.content_layout);
        noDataUtil = new NoDataUtil(mContext,view);
        mListView = (PullToRefreshListView) findViewById(R.id.chuti_list);
        mListView.init(mContext, true);
        mAdapter = new MyChuTiAdapter(mContext);
        mAdapter.setOnAdapterClickListener(this);
        mListView.setAdapter(mAdapter, true);
        mListView.setOnRefreshListener(this);
        mListView.setOnLoadDataListener(this);
        mListView.doRefresh();
    }

    private void getZhiboList() {
        if (currentPage == 0) {
            mListView.setNoNextPage();
            return;
        }
        MyQuestionListTask getDatiListTask = new MyQuestionListTask(mUserId,currentPage,size);
        getDatiListTask.addListener(myzhiboListener);
        myzhiboListTaskId = getEatShowApp().getTaskManager().addTask(getDatiListTask);
    }
        private TaskEventListener myzhiboListener = new ShowToastTaskEventListener(this) {
            @Override
            public void handleTaskCompleted(int taskId, int result, Object data) {
                super.handleTaskCompleted(taskId, result, data);
                if (myzhiboListTaskId == taskId) {
                    if (data != null){
                        List<DatiInfo> list = new ArrayList<DatiInfo>();
                        list = (List<DatiInfo>) data;
                        if (currentPage == 1){
                            mListView.onRefreshComplete();
                            mAdapter.clearData();
                            mListView.enableLoadMore();
                        }else {
                            mListView.onLoadDataComplete(true);
                        }

                        mAdapter.updateData(list);
                        currentPage = result;
                        if (mAdapter != null && mAdapter.getCount() > 0){
                            noDataUtil.ListViewNoEmpty(mListView);
                        }else {
                            noDataUtil.ListViewEmpty(mListView,null,null,-1);
                        }
                    }
                } else if (deleteZhiboTaskId == taskId) {
                    mListView.doRefresh();
                }
            }

            @Override
            public void handleTaskError(int taskId, int errorCode, String errorMsg) {
                super.handleTaskError(taskId, errorCode, errorMsg);
                if (myzhiboListTaskId == taskId) {
                    if (currentPage == 1) {
                        mListView.onRefreshComplete();
                    } else {
                        mListView.onLoadDataComplete(false);
                    }
                }
            }
        };


        @Override
        public void onPullRefresh () {

        }

        @Override
        public void onLoadMore () {
            getZhiboList();

        }

        @Override
        public void onRefresh () {
            currentPage = 1;
            getZhiboList();
        }


        @Override
        public void onDeleteClick (DatiInfo info){
            DeleteTiMuTask deleteZhiboTask = new DeleteTiMuTask(info.getQuestionId());
            deleteZhiboTask.addListener(myzhiboListener);
            deleteZhiboTaskId = getEatShowApp().getTaskManager().addTask(deleteZhiboTask);
        }
    }

